import os
import time
import pytz
import hashlib
import netifaces
from datetime import datetime


class get_os_info:
    @staticmethod
    def local_time(mode=""):
        # 设置时区为北京
        tz_beijing = pytz.timezone("Asia/Shanghai")
        current_time = datetime.now(tz_beijing)

        result = ""
        if mode == "timestamp":
            result = int(round(time.time() * 1000))  # 时间戳不受时区影响
        elif mode == "no week":
            result = current_time.strftime("%Y-%m-%d %H:%M:%S")  # 已经是24小时制
        elif mode == "continuous":
            result = current_time.strftime("%y-%m-%d-%H%M%S")  # 已经是24小时制
        elif mode == "ymd":
            result = current_time.strftime("%Y-%m-%d")  # 年-月-日
        elif mode == "hms":
            result = current_time.strftime("%H:%M:%S")  # 24小时制的时:分:秒
        elif mode == "weblook":
            result = current_time.strftime("%Y/%m/%d %H:%M:%S")
        return result

    @staticmethod
    def file_size(file_path, size_mode):
        num = os.path.getsize(file_path)
        if size_mode == "a":
            for x in ["B", "KB", "MB", "GB", "TB"]:
                if num < 1024.0:
                    return "%3.1f %s" % (num, x)
                num /= 1024.0
            return num
        elif size_mode == "b":
            return num  # 返回以bytes为单位的int类型的数字
        elif size_mode == "k":
            return str(num / 1024) + " KB"  # 返回以KB为单位的str类型的字符串,且带单位
        elif size_mode == "m":
            return (
                str(num / 1024 / 1024) + " MB"
            )  # 返回以MB为单位的str类型的字符串,且带单位
        elif size_mode == "g":
            return (
                str(num / 1024 / 1024 / 1024) + " GB"
            )  # 返回以GB为单位的str类型的字符串,且带单位
        elif size_mode == "t":
            return (
                str(num / 1024 / 1024 / 1024 / 1024) + " TB"
            )  # 返回以TB为单位的str类型的字符串,且带单位


def get_file_sha3_512(file_path):
    with open(file_path, "rb") as f:
        file_data = f.read()
        sha3_hash = hashlib.sha3_512(file_data).hexdigest()
    return sha3_hash



def get_realip():
    interfaces = netifaces.interfaces()
    for interface in interfaces:
        if interface == "lo":
            continue
        addrs = netifaces.ifaddresses(interface)
        if netifaces.AF_INET in addrs:
            ip_info = addrs[netifaces.AF_INET][0]
            ip = ip_info["addr"]
            if not ip.startswith("127.") and interface != "docker0":
                return ip
    return None